// HelloWorld.java

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Paths;

public class HelloWorld {
    // 声明本地方法
    public native void sayHello();

    public static void load() {
        try {
            // 用临时目录加载，避免不同ClassLoader加载时报重复加载的错
            File tmpFile = File.createTempFile("jntload-test", null);
            tmpFile.deleteOnExit();
            
            try(OutputStream out = new FileOutputStream(tmpFile)) {
                Files.copy(Paths.get("./libhello.so"), out);
            }
            System.out.println(tmpFile.getAbsolutePath());
            System.load(tmpFile.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static {
        // 不同ClassLoader加载时会重复初始化static
        load();
    }

    public static void main(String[] args) {
        new HelloWorld().sayHello();
    }
}